package com.lanyuan.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.ActivityFormMap;
import com.lanyuan.entity.LuckydrawFormMap;
import com.lanyuan.mapper.ActivityMapper;
import com.lanyuan.mapper.LuckydrawMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;
import com.lanyuan.util.FileUploadUtil;
import com.lanyuan.util.Kit;

/**
 * 
 * @author 抽奖活动设置
 * @Email: mmm333zzz520@163.com
 * @version 3.0v
 */
@Controller
@RequestMapping("/luckydraw/")
public class LuckydrawController extends BaseController {
	@Inject
	private LuckydrawMapper luckydrawMapper;
	@Inject
	private ActivityMapper activityMapper;

	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式

	@RequestMapping("list")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/system/luckydraw/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
		LuckydrawFormMap luckydrawFormMap = getFormMap(LuckydrawFormMap.class);
		luckydrawFormMap = toFormMap(luckydrawFormMap, pageNow, pageSize, luckydrawFormMap.getStr("orderby"));
		luckydrawFormMap.put("column", "createTime");
		luckydrawFormMap.put("sort", "desc");
		pageView.setRecords(luckydrawMapper.findUserPage(luckydrawFormMap));// 不调用默认分页,调用自已的mapper中findUserPage
		return pageView;
	}

	@SuppressWarnings("unused")
	@RequestMapping("addUI")
	public String addUI(Model model) throws Exception {
		return Common.BACKGROUND_PATH + "/system/luckydraw/add";
	}

	@ResponseBody
	@RequestMapping("addEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-新增活动")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity(HttpServletRequest request, MultipartFile file) throws Exception {
		LuckydrawFormMap luckydrawFormMap = getFormMap(LuckydrawFormMap.class, request);
		String path = request.getRealPath("/uploadFile/luckydraw");
		String filename = "luckydraw_" + Kit.formatToyyyy_MM_dd_HH_mm_ss_SSS(System.currentTimeMillis()) + "_"
				+ RandomStringUtils.randomAlphanumeric(5);
		String extensionName = FilenameUtils.getExtension(file.getOriginalFilename());
		if (!"".equals(extensionName)) {
			// 保存图片
			luckydrawFormMap.put("imgpath",
					"/uploadFile/luckydraw/" + FileUploadUtil.savePicToPath(path, filename, extensionName, file));
		}

		luckydrawFormMap.put("createTime", df.format(new Date()));// new Date()为获取当前系统时间
		luckydrawMapper.addEntity(luckydrawFormMap);
		return "success";
	}

	@RequestMapping("editUI")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty(id)) {
			model.addAttribute("luckydraw", luckydrawMapper.findbyFrist("id", id, LuckydrawFormMap.class));
		}
		return Common.BACKGROUND_PATH + "/system/luckydraw/edit";
	}

	@ResponseBody
	@RequestMapping("editEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-修改组")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request, MultipartFile file) throws Exception {
		LuckydrawFormMap luckydrawFormMap = getFormMap(LuckydrawFormMap.class, request);
		String path = request.getRealPath("/uploadFile/luckydraw");
		String filename = "luckydraw_" + Kit.formatToyyyy_MM_dd_HH_mm_ss_SSS(System.currentTimeMillis()) + "_"
				+ RandomStringUtils.randomAlphanumeric(5);
		String extensionName = FilenameUtils.getExtension(file.getOriginalFilename());
		if (!"".equals(extensionName)) {
			// 保存图片
			luckydrawFormMap.put("imgpath",
					"/uploadFile/luckydraw/" + FileUploadUtil.savePicToPath(path, filename, extensionName, file));
		} else {
			luckydrawFormMap.put("imgpath", luckydrawFormMap.get("imgfile").toString());

		}
		luckydrawFormMap.remove("imgfile");

		luckydrawFormMap.put("createTime", df.format(new Date()));// new Date()为获取当前系统时间
		luckydrawMapper.editEntity(luckydrawFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("deleteEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-删除活动")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		for (String id : ids) {
			luckydrawMapper.deleteByAttribute("id", id, LuckydrawFormMap.class);
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping("openEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-关闭活动")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String openEntity() throws Exception {
		String id = getPara("id");
		LuckydrawFormMap luckydrawFormMap = getFormMap(LuckydrawFormMap.class);
		luckydrawFormMap.put("type", "0");
		luckydrawMapper.editEntity(luckydrawFormMap);
		luckydrawFormMap = getFormMap(LuckydrawFormMap.class);
		luckydrawFormMap.put("id", id);
		luckydrawFormMap.put("type", "1");
		luckydrawMapper.editEntity(luckydrawFormMap);
		return "success";
	}

	@RequestMapping("setingUI")
	public String setingUI(Model model) throws Exception {
		String path = null;
		Map<String, Object> datamap = new HashMap<String, Object>();
		String pid = getPara("pid");
		if (Common.isNotEmpty(pid)) {
			// 查询关联详细ID
			datamap = activityMapper.findbyFrist("pid", pid, ActivityFormMap.class);
			if (datamap != null) {
				model.addAttribute("activity", datamap);
				path = "/system/activity/edit";
			} else {
				datamap = new HashMap<String, Object>();
				datamap.put("pid", pid);
				model.addAttribute("activity", datamap);
				path = "/system/activity/add";
			}

		}
		return Common.BACKGROUND_PATH + path;
	}

	@ResponseBody
	@RequestMapping("editAtEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-修改组")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editAtEntity() throws Exception {
		ActivityFormMap activityFormMap = getFormMap(ActivityFormMap.class);
		activityFormMap.put("createTime", df.format(new Date()));
		activityMapper.editEntity(activityFormMap);
		return "success";
	}

	@ResponseBody
	@RequestMapping("addAtEntity")
	@Transactional(readOnly = false)
	// 需要事务操作必须加入此注解
	@SystemLog(module = "系统管理", methods = "组管理-新增活动")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addAtEntity() throws Exception {
		ActivityFormMap activityFormMap = getFormMap(ActivityFormMap.class);
		activityFormMap.put("createTime", df.format(new Date()));
		activityMapper.addEntity(activityFormMap);
		return "success";
	}
}